<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/6
 * Time: 15:03
 */

namespace Auditing\Model;


use Think\Model;

class ClinicTableModel extends Model
{
    //自动验证
    protected $patchValidate = true;//开启批量验证
    protected $_validate = [
//        ['name','require','名称不能为空'],
        ['phone','require','电话不能为空'],
        ['big_name','require','营业执照全名不能为空'],
        ['licence_no','require','营业执照号码不能为空'],
        ['phone','number','手机号码输入不合法'],
    ];
    //自动完成
    protected $_auto = [
        ['time','time',self::MODEL_INSERT,'function'],
    ];

    public function addClinic(){
        $user_token = session('NOW_USER_TOKEN');
        $user_info = M('userdetail')->where(['token'=>$user_token])->find();
        //获取数据
        $data = I('post.');
        //判断当前用户是否已经提交过审核
        $res = $this->where(['userid'=>$user_info['userid']])->find();
        if($res){
            $this->error = '提交失败，请误重新提交审核';
            return false;
        }
        //查询是否当前用户在clinic_fail表中已存在
        //如果存在 则删除 即可以重新提交
        M('ClinicFail')->where(['user_id'=>$user_info['userid']])->delete();
        //对自定义输入的技能进行分割
        $skills = $data['skill'];
        //将其它非因为逗号字符替换成英文逗号
        $skills = str_replace('，',',',$skills);
        $skills = str_replace('、',',',$skills);
        $skills = str_replace('。',',',$skills);
        $skills = str_replace(' ',',',$skills);
        $skills = trim($skills,',');//去掉两边得，
        $this->skill = $skills;
        $this->userid = $user_info['userid'];
        //添加到数据库
        if(($clinic_id = $this->add()) === false){
            $this->error = '提交失败，请重新提交';
            return false;
        }
        //将该用户名改为诊所名称
        M('userdetail')->where(['token'=>$user_token])->setField(['username'=>I('username')]);
        //上传图片 获取对应地址
        $paths = uploadDoctor();
        $data = [];
        foreach ($paths as $k=>$path){
            switch ($k){
                case 'id_img_z':
                    $data[] = [
                        'cli_id'=>$clinic_id,
                        'flag'=>1,
                        'url'=>$path,
                    ];
                    break;
                case 'id_img_f':
                    $data[] = [
                        'cli_id'=>$clinic_id,
                        'flag'=>2,
                        'url'=>$path,
                    ];
                    break;
                case 'commpy_img':
                    $data[] = [
                        'cli_id'=>$clinic_id,
                        'flag'=>3,
                        'url'=>$path,
                    ];
                    break;
//                case 'head_img'://保存头像图片
//                    $this->where(['id'=>$clinic_id])->setField(['headurl'=>$path]);
//                    break;
            }
        }
        if(M('ClinicImg')->addAll($data) === false){
            $this->error = '图片上传失败';
            return false;
        }
        return true;
    }
}